#include "head.h"

int pwdFunc(const int cur_dir_id,char *virtualPath,char *error_buf)
{
    //建立mysql连接
    if(cur_dir_id<=0)
    {
        /* printf("id is not legal\n"); */
        strcpy(error_buf,"id is not legal!");
        return -1;
    }
    MYSQL* mysql=mysql_init(NULL);
    MYSQL* ret=mysql_real_connect(mysql,"localhost","root","123","os",0,NULL,0);
    if(ret==NULL)
    {
        sprintf(error_buf,"%s:%s","mysql_real_connect",mysql_error(mysql));
        return -1;
    }

    //通过当前文件所在目录的id找路径
    char sql1[200]={0};
    int id=cur_dir_id;
    sprintf(sql1,"select path,type from vft where id=%d;",id);
    int qret=mysql_query(mysql,sql1);
    if(qret!=0)
    {
        sprintf(error_buf,"%s:%s","mysql_query",mysql_error(mysql));
        return -1;
    }
    MYSQL_RES *rows=mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(rows);
    //如果表里不存在符合条件的一行
    if(mysql_num_rows(rows)==0)
    {
        strcpy(error_buf,"vft table don't exit the meeting criteria rows!");
        /* printf("vft table don't exit the meeting criteria rows!\n"); */
        return -1;
    }
    //如果没被删除，且是目录，就正常返回0
    char ch[]="d";
    if(!strcmp(ch,row[2]))
    {
        strcpy(error_buf,"The file corresponding to the ID is not a directory !");
        /* printf("The file corresponding to the ID is not a directory !\n"); */
        return -1; 
    }
    /* printf("%s\n",row[0]); */
    /* strcpy(virtualPath,row[0]); */
    strcpy(virtualPath,row[0]);
    mysql_close(mysql);
    return 0;
}
